home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / varia / gc-nov90.lha / gc-26nov90 / gcstring.C < prev    next >
C/C++ Source or Header  |  1990-11-01  |  913b  |  46 lines

  1. /* A string class based on the one at pg 184 of "The C++ Programming Language"
  2.    that uses heap allocated storage.
  3. */
  4.  
  5. #include "gcstring.h"
  6.  
  7. void gc_string::GCPointers() {}
  8.  
  9. gc_string::gc_string( char* chars )  {
  10.     int  len = strlen( chars );
  11.  
  12.     GCALLOCV( gc_string, len+1+sizeof( int ) );
  13.     length = len;
  14.     strcpy( &text, chars );
  15. }
  16.  
  17. gc_string::gc_string( int chars )  {
  18.     GCALLOCV( gc_string, chars+1+sizeof( int ) );
  19.     length = chars;
  20. }
  21.  
  22. char&  string::operator[]( int i )  {
  23.     if  (i < 0 ||  i >= sp->length)  {
  24.        cout << "String index out of bounds\n";
  25.        exit( 1 );
  26.     }
  27.     return  *((&sp->text)+i);
  28. }
  29.  
  30. istream&  operator>>( istream& in, string& y )  {
  31.     char  buf[ 1024 ];
  32.  
  33.     in >> buf;
  34.     y.sp = new gc_string( buf );
  35.     return  in;
  36. }
  37.     
  38. string stringplus( char* x, int xl, char* y, int yl )  {
  39.     string  xy;
  40.  
  41.     xy.sp = new gc_string( xl+yl );
  42.     strcpy( &xy.sp->text, x );
  43.     strcpy( (&xy.sp->text)+xl, y );
  44.     return  xy;
  45. }
  46.